using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;
 
public partial class JobSeekers_login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session.Contents["rcuserid"] != null)
        {
            Response.Redirect("myaccount.aspx");
        }
        if (Request.Cookies["Remember"] != null)
        {
            HttpCookie httpCookie =
            Request.Cookies["EmailAndTime"];
            txtEmail.Text = httpCookie.Values["Email"].ToString();

        }
        if (Session["RCLogin"] == null) 
            Session["RCLogin"] = 1;
         
            
        if (Convert.ToInt32(Session["RCLogin"]) >= 3)
        {
            Session["RCLogin"] =
                Convert.ToInt32(Session["RCLogin"]) + 1;
            Common common = new Common();
            string randomWord = 
            common.RandomWord(9100 + 
            Convert.ToInt32(Session["RCLogin"]));
            imageRestrict.ImageUrl = "~/RestrictLogin.ashx?word=" + randomWord;
            CompareValidator1.ValueToCompare = randomWord;             
            Restrict.Visible =  true;
        }
        else
            Restrict.Visible =  false;
            
        
    }
    
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        try
        {
            if(Session["RCLogin"] != null)
                Session["RCLogin"] =
                Convert.ToInt32(Session["RCLogin"]) + 1;    
            Login();
         }
         catch(Exception ex)
         {
            literalError.Text = ex.Message;
         }
    }
    void Login()
    {
        string[] valueToGet = new string[4] { "", "", "", "" };
        RecruitVietnam cls = new RecruitVietnam();
        cls.ConnectionString = WebConfigurationManager.ConnectionStrings[
           "RecruitVietnam"].ConnectionString;
        valueToGet = cls.LoginAuthentication(txtEmail.Text, txtPassword.Text, "1");
        if (valueToGet[0] == "OK")
        {
            Session.Add("rcuserid", valueToGet[1]);
            Session.Add("rcemail", txtEmail.Text);
            Session.Add("rccompanyname", valueToGet[2]);
            Session.Add("rcjobfree", valueToGet[4]);
            Session.Add("rcactivate", valueToGet[5]);
            WriteCookie(txtEmail.Text);
            Session.Remove("RCLogin");
            Response.Redirect("myaccount.aspx");

        }
        else
        {
            switch (valueToGet[0])
            {
                case "U":
                    lblHeader.Text = "Email is not existing," +
                    " please re-enter your email and try again.";
                    break;
                case "P":
                    lblHeader.Text = "Invalid password, " +
                    "please re-enter your own password and try again.";
                    break;
                default:
                    lblHeader.Text = "System error, " + cls.ErrorMessage;
                    break;
            }
        }
    }
    void WriteCookie(string email)
    {
        if (CheckBoxRememberEmail.Checked)
        {
            HttpCookie httpCookie = new HttpCookie("Remember", "Yes");
            Response.Cookies.Add(httpCookie);
            httpCookie = new HttpCookie("EmailAndTime");
            httpCookie.Values.Add("Email",email);
            httpCookie.Values.Add("Time", DateTime.Now.ToShortDateString());
            Response.Cookies.Add(httpCookie);
        }
    }
}
